/*
 * Copyright (c) 2024 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#include "cast_engine_dlna_event_controller.h"
#include "common_event_manager.h"
#include "common_event_support.h"
#include "hisysevent.h"
#include "if_system_ability_manager.h"
#include "ipc_skeleton.h"
#include "iservice_registry.h"
#include "system_ability_definition.h"
#include "cast_engine_log.h"

using namespace OHOS::EventFwk;
namespace OHOS {
namespace CastEngine {

DEFINE_CAST_ENGINE_LABEL("Cast-Engine-Dlna-Event-Controller");

std::shared_ptr<CastEngineDlnaEventController> CastEngineDlnaEventController::GetInstance()
{
    static std::shared_ptr<CastEngineDlnaEventController> instance_;
    if(instance_ == nullptr)
    {
        MatchingSkills matchingSkills;
        matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_CONNECTIVITY_CHANGE);
        CommonEventSubscribeInfo subscriberInfo(matchingSkills);
        instance_ = std::make_shared<CastEngineDlnaEventController>(subscriberInfo);
    }

    return instance_;
}

CastEngineDlnaEventController::SystemAbilityStatusChangeListener::SystemAbilityStatusChangeListener
        (std::weak_ptr<CastEngineDlnaEventController> defaultEventController)
: subscriber_(defaultEventController)
{

}

void CastEngineDlnaEventController::Init()
{
    if (sysAbilityListener_ != nullptr) {
        return;
    }
    sysAbilityListener_ = new (std::nothrow) SystemAbilityStatusChangeListener(weak_from_this());
    if (sysAbilityListener_ == nullptr) {
        CLOGE("Failed to create statusChangeListener due to no memory.");
        return;
    }
    sptr<ISystemAbilityManager> systemAbilityManager
        = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
    if (systemAbilityManager == nullptr) {
        CLOGE("systemAbilityManager is null");
        sysAbilityListener_ = nullptr;
        return;
    }
    int32_t ret = systemAbilityManager->SubscribeSystemAbility(COMMON_EVENT_SERVICE_ID, sysAbilityListener_);
    if (ret != ERR_OK) {
        CLOGE("subscribe system ability id: %{public}d failed", COMMON_EVENT_SERVICE_ID);
        sysAbilityListener_ = nullptr;
    }
}

void CastEngineDlnaEventController::Stop()
{
    if (sysAbilityListener_ == nullptr) {
        return;
    }
    sysAbilityListener_->Stop();
}

void CastEngineDlnaEventController::SystemAbilityStatusChangeListener::OnAddSystemAbility(
    int32_t systemAbilityId, const std::string& deviceId)
{
    if (CommonEventManager::SubscribeCommonEvent(subscriber_.lock())) {
        CLOGD("SubscribeCommonEvent ok");
    } else {
        CLOGD("SubscribeCommonEvent fail");
    }
}

void CastEngineDlnaEventController::RegisterOnConnectvityChange(std::function<void (int32_t)> func)
{
    onConnectvityChange_ = func;
}

void CastEngineDlnaEventController::OnReceiveEvent(const EventFwk::CommonEventData &data)
{
    Want want = data.GetWant();
    int32_t code = data.GetCode();
    std::string action = want.GetAction();
    CLOGD("Recieved common event:%{public}s", action.c_str());
    if (action == CommonEventSupport::COMMON_EVENT_CONNECTIVITY_CHANGE) {
        
        if (onConnectvityChange_)
        {
            onConnectvityChange_(code);
        }

        return;
    }
}

void CastEngineDlnaEventController::SystemAbilityStatusChangeListener::OnRemoveSystemAbility(
    int32_t systemAbilityId, const std::string& deviceId)
{
    CLOGW("common event service is removed.");
}

void CastEngineDlnaEventController::SystemAbilityStatusChangeListener::Stop()
{
    if (subscriber_.lock() == nullptr) {
        return;
    }
    CommonEventManager::UnSubscribeCommonEvent(subscriber_.lock());
}

} // namespace CastEngine
} // namespace OHOS
